package com.wbdgj.ui.booking;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnNoResponseListener;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

public class BookingRoomInfoActivity extends BaseActivity {

    private Context context = this;
    private String id;

    @BindView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    private ArrayList<String> localImages = new ArrayList<String>();

    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.room_window)
    TextView room_window;
    @BindView(R.id.room_size)
    TextView room_size;
    @BindView(R.id.people_number)
    TextView people_number;
    @BindView(R.id.brekker)
    TextView brekker;
    @BindView(R.id.computer_number)
    TextView computer_number;
    @BindView(R.id.network)
    TextView network;
    @BindView(R.id.bed_width)
    TextView bed_width;
    @BindView(R.id.room_rate)
    TextView room_rate;
    @BindView(R.id.old_rate)
    TextView old_rate;

    private String IS_HOUR;
    private String startDateStr;
    private String endDateStr;
    private String surplus;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_booking_room_info;
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra("id");
        IS_HOUR = getIntent().getStringExtra("IS_HOUR");
        startDateStr = getIntent().getStringExtra("startDateStr");
        endDateStr = getIntent().getStringExtra("endDateStr");
        surplus = getIntent().getStringExtra("surplus");
        getMbUserInfo();
        findViewById(R.id.ljyd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(context, BookingRoomActivity.class).putExtra("id", id)
                        .putExtra("startDateStr", startDateStr)
                        .putExtra("endDateStr", endDateStr)
                        .putExtra("surplus", surplus)
                        .putExtra("IS_HOUR", IS_HOUR));
            }
        });
    }

    private void getMbUserInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    room_type_info(""+linkedTreeMap.get("ID_CARD"));
                    room_price(""+linkedTreeMap.get("ID_CARD"));
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void room_price(String ID_CARD) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().room_price(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), ID_CARD, id, startDateStr);
        call.enqueue(new OnNoResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(IS_HOUR.equals("0")){
                        if(decimalFormat.format(linkedTreeMap.get("room_app")).equals("0")){
                            room_rate.setText((Double.parseDouble(decimalFormat.format(linkedTreeMap.get("room_rate"))) - Double.parseDouble(decimalFormat.format(linkedTreeMap.get("discount_amount"))))+"");
                            old_rate.setText("￥"+decimalFormat.format(linkedTreeMap.get("room_rate")));
                        }else{
                            old_rate.setText("￥"+decimalFormat.format(linkedTreeMap.get("room_app")));
                            room_rate.setText((Double.parseDouble(decimalFormat.format(linkedTreeMap.get("room_app"))) - Double.parseDouble(decimalFormat.format(linkedTreeMap.get("discount_amount"))))+"");
                        }
                        old_rate.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    }else{
                        room_rate.setText(linkedTreeMap.get("hour_price")+"");
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void room_type_info(String ID_CARD) {
        Call<ResultObjectModel> call01 = HttpAdapter.getSerives().room_type_info(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID), id, ID_CARD);
        call01.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")) {
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();

                    if(decimalFormat.format(linkedTreeMap.get("room_window")).equals("1")){
                        room_window.setText("有窗");
                    }else{
                        room_window.setText("无窗");
                    }

                    name.setText(linkedTreeMap.get("name")+"");
                    room_size.setText(linkedTreeMap.get("room_size")+"平米");
                    people_number.setText("可住"+ decimalFormat.format(linkedTreeMap.get("people_number"))+"人");
                    computer_number.setText( decimalFormat.format(linkedTreeMap.get("computer_number"))+"台电脑");
                    bed_width.setText("大床"+linkedTreeMap.get("bed_width")+"米");

                    if(decimalFormat.format(linkedTreeMap.get("brekker")).equals("1")){
                        brekker.setText("含早餐");
                    }else{
                        brekker.setText("无早餐");
                    }

                    if(decimalFormat.format(linkedTreeMap.get("network")).equals("1")){
                        network.setText("免费wifi");
                    }else{
                        network.setText("暂无");
                    }

                    List<LinkedTreeMap> pic = new ArrayList<>();
                    pic = (List<LinkedTreeMap>) linkedTreeMap.get("imgArr");
                    localImages = new ArrayList<>();
                    for (int i = 0; i < pic.size(); i++){
                        localImages.add(HttpAdapter.hotelUrl  +pic.get(i).get("up_path"));
                    }
                    convenientBanner.setPages(new CBViewHolderCreator() {
                        @Override
                        public Object createHolder() {
                            return new LocalImageHolderView();
                        }
                    },localImages)
                            //设置指示器是否可见
                            .setPointViewVisible(true)
                            //设置自动切换（同时设置了切换时间间隔）
                            .startTurning(3000)
                            //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                            .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused})
                            //设置指示器的方向（左、中、右）
                            .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
                            //设置点击监听事件
                            //                .setOnItemClickListener(this)
                            //设置手动影响（设置了该项无法手动切换）
                            .setManualPageable(true);

                }
            }
        });
    }

    //为了方便改写，来实现复杂布局的切换
    private class LocalImageHolderView implements Holder<String> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            //你可以通过layout文件来创建，不一定是Image，任何控件都可以进行翻页
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            Glide.with(context).load(data).placeholder(R.mipmap.detail_wait).error(R.mipmap.detail_error).into(imageView);
        }
    }
}
